package lkl.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lkl.bean.BaseResp;
import lkl.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lkl
 * @Description
 * @Date 2022/3/16
 */
@Component
public class Myfilter extends ZuulFilter {

    public Myfilter() {

        urlList.add("/tg-user/user/login");
        urlList.add("/tg-user/user/registry");
        urlList.add("/tg-user/user/getCode");
        urlList.add("/tg-user/user/validataEmail");
        urlList.add("/tg-user/user/validataloginName");
        urlList.add("/tg-user/user/registryPhone");
        urlList.add("/tg-user/user/loginPhone");
        urlList.add("/tg-user/user/findUserByToken");
        urlList.add("/tg-user/tguser/findAll");

    }

    @Autowired
    JwtUtils jwtUtils;

    List urlList = new ArrayList();

    /**
     * 什么时候过滤请求
     * PRE 在请求路由之前被调用 使用过滤器实现身份的认证
     * ROUTE 载请求路由到对应的服务后调用
     * POST 载请求路由到对应的服务之后执行
     * ERROR 在其他阶段发生错误时执行
     * @return
     */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    /**
     * 指定过滤器的执行顺序
     * @return
     */
    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER;
    }

    /**
     * 是否开启过滤器
     * @return
     */
    @Override
    public boolean shouldFilter() {

       /* RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String requestURI = request.getRequestURI();


        if(urlList.contains(requestURI)){
            return false;
        }

        return true;*/
        return false;
    }

    /**
     * 过滤器具体执行的代码
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();

        String token = request.getHeader("token");
        HttpServletResponse response = currentContext.getResponse();

        if(StringUtils.isEmpty(token)){

            BaseResp baseResp = new BaseResp(0, "未登录！", null, null);

            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            try {
                response.getWriter().write(JSONObject.toJSONString(baseResp));
            } catch (IOException e) {
                e.printStackTrace();
            }

            //停止访问
            currentContext.setSendZuulResponse(false);

        }else {

            Integer verfiy = jwtUtils.verfiy(token);

            if(verfiy==0){
                BaseResp baseResp = new BaseResp(0, "验证失败", null, null);
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json");
                try {
                    response.getWriter().write(JSONObject.toJSONString(baseResp));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //停止访问
                currentContext.setSendZuulResponse(false);
            }


        }
        //继续执行 转发到对应的微服务中执行逻辑
        return null;
    }
}
